# coding: utf-8
# 18-11-16 下午5:17


from rest_framework_jwt.settings import api_settings

jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
jwt_decode_handler = api_settings.JWT_DECODE_HANDLER


def create_token(user):
    '''
    根据user生成token
    :param user:
    :return:
    '''
    payload = jwt_payload_handler(user)
    payload['customer'] = True
    return jwt_encode_handler(payload)


def decode_token(token):
    '''
    通过token 获取 payload
    :param token:
    :return:
    {
        'user_id': user.pk,
        'email': user.email,
        'username': username,
        'exp': ''
    }
    '''
    return jwt_decode_handler(token)